package ca.uwindsor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;

//CONTAINS COMMON METHODS AND VARIABLES USED THROUGHOUT THE APP.
public class Common{

	public static String globalTerm; // used to store the sem name
	MyDBAdapter db;
	private List<Double> gpaList = new ArrayList<Double>();
	private List<Double> creditsList = new ArrayList<Double>();
	public Common() {}
	/*call this constructor for database operations*/
	public Common(MyDBAdapter db)
	{
		this.db = db;
	}

	public Date StringToDate(String sDate) {
		Date date = null;
		SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdfSource.parse(sDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public String DateToString(Date date,String format){

		SimpleDateFormat sdfDate = new SimpleDateFormat(format);
		return sdfDate.format(date);
	
		
	}

	// Returns a +ve value if date1 > date2, -ve value if date1 < date2 or 0 if
	// equal
	public int CompareDates(String date1, String date2) {
		Date d1 = this.StringToDate(date1);
		Date d2 = this.StringToDate(date2);
		int com = d1.compareTo(d2);
		return com;
	}

	public Date StringToTime(String sTime) {
		Date time = null;
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		try {
			time = df.parse(sTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}

	// Returns a +ve value if time1 > time2, -ve value if time1 < time2 or 0 if
	// equal
	public int CompareTime(String time1, String time2) {
		Date t1 = this.StringToTime(time1);
		Date t2 = this.StringToTime(time2);
		int com = t1.compareTo(t2);
		return com;
	}
	
	/*Return the letter grade for the courses, return N/A if the total marks for course
	 * is less than 100... */
	public String getLetterGrade(String courseCode)
	{
		db.setDATABASE_TABLE("marks");
		db.open();
		double marksOb = 0;
		double totalMark = 0;
		Cursor getMarks = db.getEntry("course_code",courseCode);
		
		while(getMarks.moveToNext())
		{
			marksOb += getMarks.getDouble(getMarks.getColumnIndex("marks_ob"));
			totalMark += getMarks.getDouble(getMarks.getColumnIndex("weightage"));
		}
		if(totalMark < 100){
			db.close();
			return "N/A";
		}
			
		db.setDATABASE_TABLE("grading");
		
		Cursor getGrade = db.getAllEntries();
		
		while(getGrade.moveToNext()){
			if(marksOb >= getGrade.getDouble(getGrade.getColumnIndex("start_range")) 
					&& marksOb < getGrade.getDouble(getGrade.getColumnIndex("end_range")))
			{
				db.close();
				return getGrade.getString(getGrade.getColumnIndex("letter_grade"));
			}
		}
		db.close();
		return "N/A";
	}
	
	/*Computes the GPA for the given semester*/
	
	public double computeGPA(String semName){
		db.setDATABASE_TABLE("courses");
		db.open();
		List<String> course = new ArrayList<String>();
		List<Double> credits = new ArrayList<Double>();
		List<String> grades = new ArrayList<String>();
		Cursor courses = db.getEntry("sem_name"+ semName);
		while(courses.moveToNext())
		{
			course.add(courses.getString(courses.getColumnIndex("course_code")));
			credits.add(courses.getDouble(courses.getColumnIndex("credit")));
		}
		db.close();
		for (int i=0; i<course.size(); i++)
		{
			String getGrade = getLetterGrade(course.get(i));
			if(getGrade.equals("N/A"))
			{
				credits.remove(i);
				continue;
			}
			grades.add(getGrade);
		}
		return calculateGrades(grades, credits);
	}
	
	/*calculates the gpa with grades and creits given*/
	private Double calculateGrades(List<String> grades, List<Double> credits)
	{
		db.setDATABASE_TABLE("grading");
		db.open();
		double gpa = 0;
		double totalCredits = 0;
		List<Double> points = new ArrayList<Double>();
		for(int i=0; i<grades.size(); i++)
		{
			Cursor c = db.getEntry("letter_grade", grades.get(i));
			if(c.moveToNext())
				points.add(c.getDouble(c.getColumnIndex("grade_point")));
		}
		db.close();
		for(int i=0; i<points.size(); i++)
		{
			gpa = gpa + (points.get(i)*credits.get(i));
			totalCredits = totalCredits + credits.get(i);
		}
		gpaList.add(gpa);
		creditsList.add(totalCredits);
		return gpa/totalCredits;
		
	}
	
	/*Calculates the CGPA*/
	public double computeCGPA()
	{
		double cgpa = 0.0;
		double credits = 0.0;
		if(gpaList.isEmpty())
			return 0;
		for(int i=0; i<gpaList.size();i++)
		{
			cgpa += gpaList.get(i)*creditsList.get(i);
			credits += creditsList.get(i);
		}
		return cgpa/credits;
	}
	/*chks string for vlaid integer*/
	public boolean isStringDouble(String s)
	{
		try{
			Double.parseDouble(s);
			return true;
		}
		catch(Exception e){
			return false;
		}
	}
	
}
